﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using TestingDAL.Controllers;
using TestingDAL.Models;

namespace TestingDAL.Tests.Controllers
{
    /// <summary>
    /// Summary description for AlbumControllerTest
    /// </summary>
    [TestClass]
    public class AlbumControllerTest
    {

        private MockRepository mocks;
        private IAlbumDataContext mockContext;

        [TestInitialize]
        public void SetupMocks()
        {
            mocks = new MockRepository();
            mockContext = mocks.DynamicMock<IAlbumDataContext>();
        }


        [TestMethod]
        public void Index_Returns_10_Albums()
        {
            // Arrange
            List<Album> albumList = new List<Album>();
            for (int i = 0; i < 10; i++)
            {
                albumList.Add(new Album());
            }
            using (mocks.Record())
            {
                Expect.Call(mockContext.GetAlbums()).Return(albumList);
            }

            AlbumController controller = new AlbumController(mockContext);

            using (mocks.Playback())
            {
                // Act
                ActionResult result = controller.Index();

                // Assert
                ViewResult viewResult = result as ViewResult;
                Assert.IsNotNull(viewResult, "Not a view result");

                IList<Album> list = viewResult.ViewData["Albums"]
                    as IList<Album>;
                Assert.AreEqual(10, list.Count);
                Assert.AreEqual(String.Empty, viewResult.ViewName);
            }
        }


        [TestMethod]
        public void Can_Get_Specific_Album()
        {
            // Arrange
            Album specificAlbum = new Album()
            {
                ID = 34,
                Name = "The Division Bell",
                ReleaseYear = "1994"
            };
            using (mocks.Record())
            {
                Expect.Call(mockContext.GetAlbum(0)).IgnoreArguments()
                    .Return(specificAlbum);
            }

            AlbumController controller = new AlbumController(mockContext);

            using (mocks.Playback())
            {
                // Act
                ActionResult result = controller.Individual(34);

                // Assert
                ViewResult viewResult = result as ViewResult;
                Assert.IsNotNull(viewResult, "Not a view result");

                Assert.AreEqual("The Division Bell",
                    viewResult.ViewData["Title"]);

                Album album = viewResult.ViewData["Album"] as Album;
                Assert.IsNotNull(album, "No Album provided");
                Assert.AreEqual("1994", album.ReleaseYear);
                Assert.AreEqual("The Division Bell", album.Name);
                Assert.AreEqual(34, album.ID);

                Assert.AreEqual(String.Empty, viewResult.ViewName);
            }
        }
    }
}
